home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 197 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointers to structures
  5. Date: 3 Jan 1996 06:31:23 GMT
  6. Organization: Kalevi, Inc
  7. Message-ID: <4cd7rr$aae@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe4.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Jan 02, 1996 21:59:00 in article <Pointers to structures>,
  15. 'icarus@loomis (Tel Janin Aellinsar)' wrote: 
  16.  
  17.  
  18. >I'm having trouble using a pointer to a struct.  The idea is to have a  
  19. >struct get filled by a function, which gets the address and everything  
  20. >via function(struct type*).  Very straightforward.  However, if I try to  
  21. >CHANGE anything in the struct, it just goes back when the function is  
  22. >over.  So, let's pretend this imaginary structure is what I'm using: 
  23. >struct st1 { 
  24. >char *name; 
  25. >int yadda; 
  26. >}; 
  27. >And the function is: 
  28. >fn1(struct st1 *st) 
  29. >{ 
  30. >st = (struct st1*)malloc(sizeof(struct st1*)); 
  31.  
  32. You're allocating memory for a pointer (4 bytes on most 
  33. machines).  Change it to: 
  34.   st = malloc(sizeof(struct st1)); 
  35.  
  36. (Note: I dropped the cast also as it shouldn't be necessary). 
  37.  
  38. -- 
  39.  
  40. Pete 
  41.  
  42.  
  43.  
  44.  
  45.  
  46.